package com.example.onlineeducation.ui.offline

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.data.constant.OfflineStudyLiveState
import com.example.onlineeducation.data.entity.OfflineStudyLiveListEntity
import com.example.onlineeducation.databinding.OfflineStudyListAcBinding
import com.example.onlineeducation.databinding.OfflineStudyListItemBinding
import com.example.onlineeducation.viewmodel.OfflineStudyListAcVM
import com.yc.jetpacklib.extension.showToast
import com.yc.jetpacklib.extension.ycLoadImageNet
import com.yc.jetpacklib.extension.ycLoadImageRes
import com.yc.jetpacklib.extension.ycSetTextColorRes
import com.yc.jetpacklib.net.doSuccess
import com.yc.jetpacklib.recycleView.YcDecoration
import com.yc.jetpacklib.recycleView.YcRecyclerViewAdapter
import com.yc.jetpacklib.refresh.YcRefreshSpecialUtil
import com.yc.jetpacklib.release.YcSpecialViewSmart
import com.yc.jetpacklib.utils.YcResources
import com.yc.jetpacklib.utils.ycDp

/**
 * Creator: yc
 * Date: 2022/4/1 15:10
 * UseDes:线下学习活动列表
 */
@SuppressLint("SetTextI18n")
class OfflineStudyListAc : BaseActivity<OfflineStudyListAcBinding>(OfflineStudyListAcBinding::inflate) {
    companion object {
        private const val KEY_SHOW_ALL = "key_show_all"

        /*
        * showAll:显示所有活动
        * */
        @JvmStatic
        fun newInstance(context: Context, showAll: Boolean = true) {
            val intent = Intent(context, OfflineStudyListAc::class.java).apply {
                putExtra(KEY_SHOW_ALL, showAll)
            }
            context.startActivity(intent)
        }
    }

    private var mShowAll = true
    private val mViewModel by ycViewModels<OfflineStudyListAcVM>()
    private val mAdapter by YcRecyclerViewAdapter.ycLazyInitApply<OfflineStudyLiveListEntity, OfflineStudyListItemBinding>(OfflineStudyListItemBinding::inflate) {
        mOnUpdate = {
            ivStudy.ycLoadImageNet(it.imgUrl, errorImg = R.drawable.ic_topic)
            tvStudyProName.text = it.proName
            tvStudyName.text = it.studyLiveName
            tvStudyTimeStart.text = "开始：${it.studyLiveTimeStart}"
            tvStudyTimeEnd.text = "结束：${it.studyLiveTimeEnd}"
            tvState.text = OfflineStudyLiveState.getStudyLiveStateName(it.studyLiveState)
            tvState.ycSetTextColorRes(OfflineStudyLiveState.getStudyLiveStateTvColor(it.studyLiveState))
            ivState.ycLoadImageRes(OfflineStudyLiveState.getStudyLiveStateDrawable(it.studyLiveState))
        }
        mItemClick = {
            if (it.studyLiveOnlineState == OfflineStudyLiveState.ONLINE) {
                OfflineStudyDetailAc.newInstance(this@OfflineStudyListAc, mProId, it.studyLiveId, mIsProMonitor)
            } else {
                showToast("该活动已下线")
            }
        }
    }

    private lateinit var mRefreshUtil: YcRefreshSpecialUtil
    private lateinit var mProId: String

    /**
     * 是否是班组长
     */
    private var mIsProMonitor: Boolean = false

    override fun OfflineStudyListAcBinding.initView() {
        mShowAll = intent.getBooleanExtra(KEY_SHOW_ALL, true)
        if (mShowAll) {
            actionBar.setTitle("学习活动")
        } else {
            actionBar.setTitle("已参加的活动")
        }

        rv.adapter = mAdapter
        rv.addItemDecoration(YcDecoration(1.ycDp()).apply {
            setLineColor(YcResources.getColorRes(R.color.every_lib_line))
        })
        srl.setNoMoreData(true)
        mRefreshUtil = YcRefreshSpecialUtil(this@OfflineStudyListAc, srl, mAdapter, YcSpecialViewSmart(rv, fl)).apply {
            mRefreshAndLoadMore = { isRefresh: Boolean, pageIndex: Int, pageSize: Int ->
                if (isRefresh) {
                    getData()
                }
            }
        }
        App.mInstant.mViewModel.mProInfoEntity.observe {
            mProId = it!!.proId
            mIsProMonitor = it.isProMonitor
        }

        mViewModel.apply {
            mGetStudyLiveList.observe {
                it.doSuccess {
                    mAdapter.addAllData(it)
                }.ycShowNetError()
                mRefreshUtil.ycRefreshAndLoadMoreResult(it)
            }
        }
    }

    private fun getData() {
        if (mShowAll) {
            mViewModel.getStudyLiveList(mProId)
        } else {
            mViewModel.getOfflineJoinActiveList(mProId)
        }
    }
}